﻿using System;
using System.IO;
using Tamir.SharpSsh.jsch;
using Tamir.Streams;
using Tamir.SharpSsh;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;


namespace SSHTunnel
{
    class Program
    {
        public static string Host = "10.0.0.2";
        public static string User = "dahai";
        public static string Pass = User;
        static void Main(string[] args)
        {
            try
            {
                SshShell shell = new SshShell(Host, User, Pass);

                //This statement must be prior to connecting
                //shell.RedirectToConsole();

                Console.Write("Connecting...");
                shell.Connect();
                Console.WriteLine("OK");

                //shell.RemoveTerminalEmulationCharacters = true;

                if (shell.ShellOpened)
                {
                    Console.WriteLine(shell.Expect("dahai@linuxqa"));

                    shell.WriteLine("ssh -g -L 7997:rx1620-5.test:80 root@rx1620-5.test");
                    Console.Write(shell.Expect("password"));

                    shell.WriteLine("iforgot");
                    Console.Write(shell.Expect("root@rx1620-5"));
                    
                    while (shell.ShellOpened)
                    {
                        string command = Console.ReadLine();
                        if (command == "exit") break;
                    }
                }
                Console.Write("Disconnecting...");

                shell.WriteLine("exit");
                Console.Write(shell.Expect("dahai@linuxqa"));

                shell.WriteLine("exit");
                shell.Close();
                Console.WriteLine("OK");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:"+e.Message);
            }
        }
    }
}
